//
//  ADWelComeController.swift
//  myBlog
//
//  Created by mac2014 on 16/3/24.
//  Copyright © 2016年 AD. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit
//程序启动时的欢迎界面

class ADWelComeController: UIViewController {
    ///  mark - properties
    
    ///  nark - system functions
    override func viewDidLoad() {
        super.viewDidLoad()
        
        prepareUI()
        
        // 获取用户信息
        if let urlString = ADUserAcountViewModel.sharedInstance.userAccount?.avatar_large {
            let url = NSURL(string: urlString)
            self.logoView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
        }
        
        //获取用户的头像信息
        ADUserAcountViewModel.sharedInstance.loadUserInfo { (error) -> () in
            if error != nil {
                
                return
            }
            //如果没有报错，给头像赋值
            if let urlString = ADUserAcountViewModel.sharedInstance.userAccount?.avatar_large {
                let url = NSURL(string: urlString)
                self.logoView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
            }
        }

    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        moveAnimation()
    }
    
    

    ///  mark - custom functions
    //添加约束
    func prepareUI() {
        //添加控件
        view.addSubview(bgImageView)
        view.addSubview(logoView)
        view.addSubview(welcomeLabel)
        
        //添加约束
        bgImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(view)
        }
        logoView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-160)
        }
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.top.equalTo(logoView.snp_bottom).offset(16)
        }
    }

    func moveAnimation() {
        UIView.animateWithDuration(1, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.logoView.snp_updateConstraints(closure: { (make) -> Void in
                make.bottom.equalTo(self.view).offset(-(UIScreen.mainScreen().bounds.height - 200))
            })
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(0.5, animations: { () -> Void in
                    self.welcomeLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        //切换界面
                        AppDelegate.jumpToSpecifiedViewController(ADMainController())
                })
        }
    }
    
    /// mark - 懒加载
    //背景图片
    lazy var bgImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    //logoView
    lazy var logoView: UIImageView = {
        let logoView = UIImageView(image: UIImage(named: "avatar_default_big"))
        logoView.layer.cornerRadius = 42.5
        //masksToBounds=true允许对边框做效果
        logoView.layer.masksToBounds = true
        return logoView
}()

    //欢迎标语
    lazy var welcomeLabel: UILabel = {
        let label = UILabel()
        label.text = "欢迎回来"
        label.font = UIFont.systemFontOfSize(18)
        label.sizeToFit()
        label.alpha = 0
        return label
    }()
    
}
